﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OnlineShop
{
    /// <summary>
    /// Interaction logic for LoginUser.xaml
    /// </summary>
    public partial class LoginUser : Window
    {
        public LoginUser()
        {
            InitializeComponent();
        }

        private void OnBackToStartMenuClick(object sender, RoutedEventArgs e)
        {
            StartMenu window = new StartMenu();
            window.Show();
            this.Close();
        }

        private void OnLoginUserClick(object sender, RoutedEventArgs e)
        {
            StreamReader reader;
            string pass;
            string name;
            if (File.Exists("Users\\" + this.name.Text + ".txt"))
            {
                reader = new StreamReader("Users\\" + this.name.Text + ".txt");
                string[] text;
                using (reader)
                {
                    text = reader.ReadToEnd().Split(new char[] {':'}, StringSplitOptions.RemoveEmptyEntries);
                }
                pass = text[3];
                name = text[1];

                if (name == this.name.Text && pass == this.password.Password)
                {
                    UserAddMenu window = new UserAddMenu(name, text[5]);
                    window.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Invalid username or password");
                    return;
                }
            }
            else
            {
                MessageBox.Show("Invalid username or password");
                return;
            }
             
            
        }

    }
}
